/**
 * ModuleService.java
 */
package org.yate.oa.service.imp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.yate.oa.dao.IBaseMapperDao;
import org.yate.oa.dao.mapper.RoleMapper;
import org.yate.oa.dao.mapper.UserMapper;
import org.yate.oa.dao.mapper.UserRefRoleMapper;
import org.yate.oa.model.UserRefRoleEntity;
import org.yate.oa.service.IUserRefRoleService;

/**
 * @author Yate
 * @date Nov 13, 2013
 * @description TODO
 * @version 1.0
 */
@Service
public class UserRefRoleService extends BaseService<UserRefRoleEntity, Long> implements IUserRefRoleService {

	@Resource(name = "userMapper")
	private UserMapper userDao;

	@Resource(name = "roleMapper")
	private RoleMapper roleMapper;

	@Resource(name = "userRefRoleMapper")
	private UserRefRoleMapper urrDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.yate.oa.service.IUserRefRoleService#getEntityByRoleId(long)
	 */
	@Override
	public List<UserRefRoleEntity> getEntityByRoleId(long rid) {
		List<UserRefRoleEntity> ds = this.urrDao.getEntityByRoleId(rid);
		for (UserRefRoleEntity e : ds) {
			e.setUser(userDao.getEntitiesByURId(e.getUrId()));
			e.setRole(roleMapper.getEntitiesByURId(e.getUrId()));
		}
		return ds;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.yate.oa.service.IUserRefRoleService#removeByRoleId(long)
	 */
	@Override
	public void removeByRoleId(long rid) {
		this.urrDao.removeByRoleId(rid);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.yate.oa.service.IUserRefRoleService#batchAdd(java.util.List)
	 */
	@Override
	public void batchAdd(long rid, String... uids) {
		List<UserRefRoleEntity> ds = new ArrayList<UserRefRoleEntity>();
		for (String uid : uids) {
			ds.add(new UserRefRoleEntity(NumberUtils.toInt(uid), rid));
		}
		this.urrDao.batchAdd(ds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.yate.oa.service.imp.BaseService#getMapperDao()
	 */
	@Override
	protected IBaseMapperDao<UserRefRoleEntity, Long> getMapperDao() {
		return this.urrDao;
	}

}
